﻿#include <QDebug>
#include <QVector>
#include <QVector2D>
#include "MyAnimation.h"


MyAnimation::MyAnimation(QObject *parent) : QAbstractAnimation(parent), ball(parent)
{
//    auto test = ball.createNewAccAnima();
//    test->setDuration(1000);
//    test->setStartValue(QVector2D(100,1));
//    test->setEndValue(QVector2D(500,2));
//    test->start(QAbstractAnimation::DeleteWhenStopped);

}

int MyAnimation::duration() const
{
    return -1;
}

void MyAnimation::updateCurrentTime(int currentTime)
{
    Q_UNUSED(currentTime)
    const double delta_t_s = 1.0 / 60.0;
    volatile double acceleration = ball.sumOfComponentsAcc().x(); // 恒力牵引
// 分析阻力加速度
    // 运动方向
    if (qAbs(ball.velocity) > ball.resistAcc * delta_t_s) // 如果有速度（计算机表示出的速度值是离散的）
    {
        if (ball.velocity > 0)
            acceleration -= ball.resistAcc;
        else
            acceleration += ball.resistAcc;
    }
    // 运动趋势
    else
    {
        if (qAbs(acceleration) <= ball.resistAcc)
        {
            ball.velocity = 0;
            return;
        }
        else if (acceleration > 0)
        {
            acceleration -= ball.resistAcc;
        }
        else
        {
            acceleration += ball.resistAcc;
        }
    }
    ball.velocity += acceleration * delta_t_s;
    ball.offsetSum += ball.velocity * delta_t_s;
    int offsetInt = int(ball.offsetSum);
    if (offsetInt >= 1 || offsetInt <= -1)
    {
        ball.frame.translate(offsetInt, 0);
        ball.offsetSum -= offsetInt;
    }
    emit valueChanged();
}
